package com.lineage.data.event;

import com.lineage.data.executor.EventExecutor;
import com.lineage.server.datatables.lock.AccountBankReading;
import com.lineage.server.templates.L1Bank;
import com.lineage.server.templates.L1Event;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.RangeLong;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BankSet extends EventExecutor {
  private static final Log _log = LogFactory.getLog(BankSet.class);
  
  public static long BANKMAX;
  
  public static long BANK_INTEREST_OVER;
  
  public static int BANK_ITEMID;
  
  public static int BANK_TIME = 1;
  
  public static double BANK_INTEREST = 0.01D;
  
  public static EventExecutor get() {
    return new BankSet();
  }
  
  public void execute(L1Event event) {
    try {
      String[] set = event.get_eventother().split(",");
      try {
        BANKMAX = Long.parseLong(set[0]);
      } catch (Exception e2) {
        BANKMAX = 1900000000L;
        _log.error("未设定银行储蓄上限(使用预设19亿)");
      } 
      try {
        BANK_INTEREST_OVER = Long.parseLong(set[1]);
      } catch (Exception e2) {
        BANK_INTEREST_OVER = 2000000000L;
        _log.error("未设定利息及现存款总额(使用预设20亿)");
      } 
      try {
        BANK_ITEMID = Integer.parseInt(set[2]);
      } catch (Exception e2) {
        BANK_ITEMID = 40308;
        _log.error("未设定存款物品编号(使用预设40308)");
      } 
      try {
        BANK_TIME = Integer.parseInt(set[3]);
      } catch (Exception e2) {
        BANK_TIME = 60;
        _log.error("未设银行计算利息时间(使用预设60分钟)");
      } 
      try {
        BANK_INTEREST = Double.parseDouble(set[4]);
      } catch (Exception e2) {
        BANK_INTEREST = 0.01D;
        _log.error("未设银行利息比率(使用预设0.01%)");
      } 
      _log.info("银行设置\n 帐户储蓄上限: " + RangeLong.scount(BANKMAX) + " \n 存款总额限制: " + 
          RangeLong.scount(BANK_INTEREST_OVER) + "\n 存款物品编号: " + 
          RangeLong.scount(BANK_INTEREST_OVER) + "\n 利息计算时间: " + BANK_TIME + "\n 利息比率: " + 
          BANK_INTEREST);
      AccountBankReading.get().load();
      BankTimer bankTime = new BankTimer();
      bankTime.start();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private class BankTimer extends TimerTask {
    private ScheduledFuture<?> _timer;
    
    private BankTimer() {}
    
    public void start() {
      int timeMillis = BankSet.BANK_TIME * 60 * 1000;
      this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, timeMillis, timeMillis);
    }
    
    public void run() {
      try {
        Map<String, L1Bank> map = AccountBankReading.get().map();
        if (map.isEmpty())
          return; 
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
          String key = iterator.next();
          L1Bank bank = map.get(key);
          if (bank.isLan() && !bank.isEmpty()) {
            long nwecount = bank.get_adena_count() + 
              Math.round(bank.get_adena_count() * BankSet.BANK_INTEREST);
            bank.set_adena_count(nwecount);
            AccountBankReading.get().updateAdena(bank.get_account_name(), nwecount);
            Thread.sleep(5L);
          } 
        } 
      } catch (Exception e) {
        BankSet._log.error("银行管理员时间轴异常重启", e);
        GeneralThreadPool.get().cancel(this._timer, false);
        BankTimer bankTime = new BankTimer();
        bankTime.start();
      } 
    }
  }
}
